package com.example.graduation.controller.address;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.graduation.entity.pojo.Address;
import com.example.graduation.service.AddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

/**
 * @author Jeffery
 */
@RequestMapping("/address")
@RestController
@Api(tags = "地址中心")
@Slf4j(topic = "AddressController")
public class AddressController {
    private static final Integer PAGE_SIZE = 5;

    @Resource
    private AddressService addressService;

    /**
     * 新增地址
     *
     * @param addressName 地址名称
     * @return html
     */
    @PostMapping("/saveAddress")
    @ApiImplicitParam(name = "addressName", value = "地址名称")
    public String saveAddress(@RequestParam("addressName") String addressName,
                              Model model) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("address_name", addressName);
        Address address = addressService.getOne(wrapper);
        String resultMsg;

        if (address != null) {
            resultMsg = "该地址已经存在！";
        } else {
            address = new Address();
            address.setAddressId(UUID.randomUUID().toString());
            address.setAddressName(addressName);
            address.setCreateTime(new Timestamp(System.currentTimeMillis()));

            addressService.save(address);
            log.info("地址新增成功！");
            resultMsg = "地址新增成功！";
        }
        model.addAttribute("result_msg", resultMsg);
        return "";
    }

    /**
     * 分页查询地址
     *
     * @param pageNumber 页码
     * @return html
     */
    @PostMapping("/getAddressPageList/{pageNumber}")
    public String getAddressPageList(@PathVariable("pageNumber") Integer pageNumber,
                                     Model model) {
        List<Address> addressList;
        PageHelper.startPage(pageNumber, PAGE_SIZE);
        addressList = addressService.list();
        PageInfo<Address> pageInfo = new PageInfo<>(addressList);
        model.addAttribute("pageInfo", pageInfo);
        return "";
    }

    /**
     * 模糊查询地址
     *
     * @param jsonObject json条件
     * @return html
     */
    @PostMapping("/getAddressList")
    public String getAddressList(@RequestBody JSONObject jsonObject,
                                 Model model) {
        Address address = jsonObject.toJavaObject(Address.class);
        QueryWrapper<Address> wrapper = new QueryWrapper<>();

        wrapper.like("address_id", address.getAddressId());
        wrapper.like("address_name", address.getAddressName());
        wrapper.like("address_create_time", address.getCreateTime());
        wrapper.like("address_update_time", address.getUpdateTime());
        wrapper.like("address_version", address.getVersion());
        wrapper.like("address_is_deleted", address.getIsDeleted());

        List<Address> addressList = addressService.list(wrapper);
        log.info("addressList : {}", addressList);
        model.addAttribute("addressList", addressList);
        return "";
    }

}
